package main

//$ curl -i http://localhost:9999/
//HTTP/1.1 200 OK
//Date: Mon, 12 Aug 2019 16:52:52 GMT
//Content-Length: 18
//Content-Type: text/html; charset=utf-8
//<h1>Hello Gee</h1>

import (
	"fmt"
	"gee"
	"log"
	"net/http"
	"time"
)

func onlyForV2() gee.HandlerFunc {
	return func(c *gee.Context) {
		t := time.Now()
		c.Fail(500, "Internal Server Error")
		log.Printf("[%d] %s in %v for group v2", c.StatusCode, c.Req.RequestURI, time.Since(t))
	}
}

type student struct {
	Name string
	Age  int8
}

func FormatAsDate(t time.Time) string {
	year, month, day := t.Date()
	return fmt.Sprintf("%d-%02d-%02d", year, month, day)
}

func main() {
	//r := gee.New()
	//r.Use(gee.Logger(),gee.Recovery())
	//r.SetFuncMap(template.FuncMap{
	//	"FormatAsDate": FormatAsDate,
	//})
	//r.LoadHTMLGlob("templates/*")
	//r.Static("/assets", "./static")
	//
	//stu1 := &student{Name: "Geektutu", Age: 20}
	//stu2 := &student{Name: "Jack", Age: 22}
	//r.GET("/", func(c *gee.Context) {
	//	c.ToHTML(http.StatusOK, "css.tmpl", nil)
	//})
	//r.GET("/panic", func(c *gee.Context) {
	//	names := []string{"geektutu"}
	//	c.String(http.StatusOK, names[100])
	//})
	//r.GET("/students", func(c *gee.Context) {
	//	c.ToHTML(http.StatusOK, "arr.tmpl", gee.H{
	//		"title":  "gee",
	//		"stuArr": [2]*student{stu1, stu2},
	//	})
	//})
	//// 新建一个分组路由
	//v1 := r.Group("/v1")
	//{
	//	v1.GET("/date", func(c *gee.Context) {
	//		c.ToHTML(http.StatusOK, "custom_func.tmpl", gee.H{
	//			"title": "gee",
	//			"now":   time.Date(2019, 8, 17, 0, 0, 0, 0, time.UTC),
	//		})
	//	})
	//
	//	v1.GET("/hello", func(c *gee.Context) {
	//		// expect /hello?name=geektutu
	//		c.String(http.StatusOK, "hello %s, you're at %s\n", c.GetQueryParmas("name"), c.Path)
	//	})
	//}
	//v2 := r.Group("/v2")
	////v2.Use(onlyForV2()) // 只给v2分组使用该中间件
	//{
	//	v2.GET("/hello/:name", func(c *gee.Context) {
	//		// expect /hello/geektutu
	//		c.String(http.StatusOK, "hello %s, you're at %s\n", c.Param("name"), c.Path)
	//	})
	//	v2.POST("/login", func(c *gee.Context) {
	//		c.ToJSON(http.StatusOK, gee.H{
	//			"username": c.PostForm("username"),
	//			"password": c.PostForm("password"),
	//		})
	//	})
	//
	//}
	r := gee.New()
	r.Use(gee.Logger(), gee.Recovery())
	r.GET("/", func(c *gee.Context) {
		c.String(http.StatusOK, "Hello Geektutu\n")
	})
	// index out of range for testing Recovery()
	r.GET("/panic", func(c *gee.Context) {
		names := []string{"geektutu"}
		c.String(http.StatusOK, names[100])
	})

	r.Run(":9999")
}
